package com.xinxin.base.entity;

import lombok.Getter;
import org.springframework.http.HttpStatus;

import java.io.Serializable;

/**
 * @author ys
 * @date 2021/5/18
 */
@Getter
public class ApiResult<T> implements Serializable {

    private static final long serialVersionUID = -3956151014956197842L;

    private Integer code;
    private String msg;
    private T data;

    public static <T> ApiResult<T> success(T data) {
        ApiResult<T> result = success();
        result.data = data;
        return result;
    }

    public static <T> ApiResult<T> success() {
        return code(HttpStatus.OK);
    }

    public static <T> ApiResult<T> fail(String msg) {
        ApiResult<T> result = fail();
        return result.msg(msg);
    }

    public static <T> ApiResult<T> fail() {
        return code(HttpStatus.INTERNAL_SERVER_ERROR);
    }

    public static <T> ApiResult<T> fail(ResultCode resultCode) {
        ApiResult<T> result = code(resultCode.getCode());
        return result.msg(resultCode.getMsg());
    }

    public static <T> ApiResult<T> code(HttpStatus status) {
        return code(status.value());
    }

    public static <T> ApiResult<T> code(Integer code) {
        ApiResult<T> result = new ApiResult<>();
        result.code = code;
        return result;
    }

    public ApiResult<T> msg(String msg) {
        this.msg = msg;
        return this;
    }
}
